# $Id: Main.pm 6 2008-06-16 18:04:51Z longerdev $
=head1 COPYRIGHT

Copyright (C) 2008 Longer <longerdev@gmail.com>

=head1 LICENSE

This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU Lesser General Public License
along with this program.  If not, see <http://www.gnu.org/licenses/>.

=cut

package RL::Main;

use strict;
use Data::Dumper;

use Socket;


sub new{
	my $self = {};
	$self->{'login'} = $_[1];
	$self->{'pass'} = $_[2];
	
	my $cpass = "";
	for (my $i=0;$i<length($self->{'pass'});$i++){
		$cpass .= sprintf "%%%x",ord(substr($self->{'pass'},$i,1));
	}
	
	$self->{'cookie'} = "user=".$self->{'login'}."-".uc($cpass);
	
	# NOTE: 0 - STDOUT; 1 - Append; 2 - NewFile;
	$self->{'wr'}{'mode'} = 0;
	
	$self->{'iaddr'} = inet_aton("rapidshare.com");
	
	return bless $self;
}

sub setWrMode{
	my $self = shift;
	$self->{'wr'}{'mode'} = shift;
	$self->{'wr'}{'fn'} = shift;
	return;
}

sub fromCmd{
	my $self = shift;
	my $cmd = shift;
	
	if ($self->{'wr'}{'mode'}){
		if ($self->{'wr'}{'mode'} == 1){
			open(FOUT, '>>'.$self->{'wr'}{'fn'});
		}
		else{
			open(FOUT, '>'.$self->{'wr'}{'fn'});
		}
		
		foreach my $l (@$cmd){
			my $dl = $self->getDirectLink($l);
			if ($dl){
				print FOUT $dl."\n";
			}
			else{
				print STDERR "Not Found: $l\n";
			}
		}
		
		close(FOUT);
	}
	else{
		foreach my $l (@$cmd){
			my $dl = $self->getDirectLink($l);
			if ($dl){
				print $dl."\n";
			}
			else{
				print STDERR "Not Found: $l\n";
			}
		}
	}
}

sub fromFile{
	my $self = shift;
	my $fn = shift;
	
	if ($self->{'wr'}{'mode'}){
		if ($self->{'wr'}{'mode'} == 1){
			open(FOUT, '>>'.$self->{'wr'}{'fn'});
		}
		else{
			open(FOUT, '>'.$self->{'wr'}{'fn'});
		}
		
		open(FH, $fn);
		while (<FH>){
			if (not $_ =~ /^http:\/\/rapidshare\.com\//){
				next;
			}
			$_ =~ s/\n$//;
			my $l = $_;
			my $dl = $self->getDirectLink($l);
			if ($dl){
				print FOUT $dl."\n";
			}
			else{
				print STDERR "Not Found: $l\n";
			}
		}
		close(FH);
		
		close(FOUT);
	}
	else{
		open(FH, $fn);
		while (<FH>){
			if (not $_ =~ /^http:\/\/rapidshare\.com\//){
				next;
			}
			$_ =~ s/\n$//;
			my $l = $_;
			my $dl = $self->getDirectLink($l);
			if ($dl){
				print $dl."\n";
			}
			else{
				print STDERR "Not Found: $l\n";
			}
		}
		close(FH);
	}
}

sub getDirectLink{
	my $self = shift;
	my $url = shift;
	
	if ($url =~ /^http:\/\/rapidshare\.com\//){
		$url =~ s/^http:\/\/rapidshare\.com//;
		#$url =~ s/\n$//;
	}
	else{
		print STDERR "Bad url: $url";
		return;
	}
	
	my $host = "rapidshare.com";
	#my $host = '195.122.131.16';
	my $port = 80;
	
	my $paddr = sockaddr_in($port, $self->{'iaddr'});
	
	socket(SOCK, PF_INET, SOCK_STREAM, getprotobyname('tcp'));
	connect(SOCK, $paddr);
	
	send(SOCK,"GET $url HTTP/1.0\r\nHost: $host\r\nCookie: ".$self->{'cookie'}.";\r\nUser-Agent: Opera/9.50 (X11; Linux i686; U; en)\r\n\r\n",0);
	#send(SOCK,"GET / HTTP/1.0\r\nHost: rapidshare.com\r\n\r\n",0);
	my $subhost = "";
	my $subfn = "";
	while(<SOCK>){
		#die 123;
		#print $_;
		if ($_ =~ m/<form action="(http:\/\/)(.+)" method="post">/){
			$2 =~ m/^([a-z0-9]+)\.rapidshare\.com\/(.+)/;
			$subhost = $1.".rapidshare.com";
			$subfn = "/".$2;
			#print $subhost.$subfn;
			last;
		}
	}
	close(SOCK);
	my $dl = "";
	if ($subhost ne "" && $subfn ne ""){
		#my @files;
		#print $subhost.$subfn;
		my $siaddr = inet_aton($subhost);
		my $spaddr = sockaddr_in($port, $siaddr);
		socket(SOCK, PF_INET, SOCK_STREAM, getprotobyname('tcp'));
		connect(SOCK, $spaddr);
		send(SOCK,"POST $subfn HTTP/1.0\r\nUser-Agent: Opera/9.50 (X11; Linux i686; U; en)\r\nHost: $subhost\r\nCookie: ". $self->{'cookie'}.";\r\nContent-Length: 16\r\nContent-Type: application/x-www-form-urlencoded\r\n\r\ndl.start=PREMIUM",0);
		while(<SOCK>){
			#print $_;
			if ($_ =~ m/<form name="dlf" action="http:\/\/([a-z0-9]+)\.rapidshare.com\/files\/(.+)" method="post">/){
				$dl = "http://".$self->{'login'}.":".$self->{'pass'}."\@$1.rapidshare.com/files/$2";
				last;
			}
		}
		close(SOCK);
	}
	
	return $dl;
}

sub getRLinks{
	my $self = shift;
	my $url = shift;
	
	# TODO: get port
	$url =~ m/^http\:\/\/([^\/]+)(\/.*)$/;
	
	my $host = $1;
	my $port = 80;
	my $path = $2;
	
	my $iaddr = inet_aton($host);
	my $paddr = sockaddr_in($port, $iaddr);
	
	socket(SOCK, PF_INET, SOCK_STREAM, getprotobyname('tcp'));
	connect(SOCK, $paddr);
	
	send(SOCK,"GET $path HTTP/1.0\r\nHost: $host\r\nUser-Agent: Opera/9.50 (X11; Linux i686; U; en)\r\n\r\n",0);
	
	if ($self->{'wr'}{'mode'}){
		if ($self->{'wr'}{'mode'} == 1){
			open(FOUT, '>>'.$self->{'wr'}{'fn'});
		}
		else{
			open(FOUT, '>'.$self->{'wr'}{'fn'});
		}
		
		while(<SOCK>){
			#print $_;
			my $tmp = $_;
			while ($tmp =~ m/http:\/\/rapidshare\.com\/files\/([A-Za-z0-9.,\-_+'\/]+)/){
				print FOUT "http://rapidshare.com/files/$1\n";
				$tmp =~ s/http:\/\/rapidshare\.com\/files\/[A-Za-z0-9.,\-_+'\/]+//;
			}
		}
		
		close(FOUT);
	}
	else{
		while(<SOCK>){
			#print $_;
			my $tmp = $_;
			while ($tmp =~ m/http:\/\/rapidshare\.com\/files\/([A-Za-z0-9.,\-_+'\/]+)/){
				print "http://rapidshare.com/files/$1\n";
				$tmp =~ s/http:\/\/rapidshare\.com\/files\/[A-Za-z0-9.,-_+']+//;
			}
		}
	}
	close(SOCK);
}


1;